<?php
class Emptopositions_model extends CRM_Model{
	private $typeDocRid;
	public function __construct(){
		parent::__construct();
		$typeDocRow = $this->GetTypeDoc();
		$this->typeDocRid = $typeDocRow->rid; 
	}
	
	public function GetDS(){
		$this->db->select('_documents.rid, 
							DATE_FORMAT(_emp_to_positions_headers.date_doc, \'%d.%m.%Y\') as date_doc, 
							_emp_to_positions_rows._employeers_rid as _employeers_rid,
							_emp_to_positions_rows._positions_rid as _positions_rid,
							DATE_FORMAT(_emp_to_positions_rows.bdate, \'%d.%m.%Y\') as bdate,
							CONCAT(_employeers.f_name,\' \',_employeers.s_name,\' \',_employeers.l_name) as employeer_name,
							_positions.name as position_name,
							_filials.name as filial_name,
							DATE_FORMAT(_documents.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_emp_to_positions_headers', '_emp_to_positions_headers._documents_rid = _documents.rid');
		$this->db->join('_emp_to_positions_rows', '_emp_to_positions_rows._emp_to_positions_headers_rid = _emp_to_positions_headers.rid');
		$this->db->join('_employeers', '_emp_to_positions_rows._employeers_rid = _employeers.rid');
		$this->db->join('_filials', '_employeers._filials_rid = _filials.rid');
		$this->db->join('_positions', '_emp_to_positions_rows._positions_rid = _positions.rid');
		$this->db->where(array('_documents._documents_types_rid'=>$this->typeDocRid));
		if($searchRule = $this->ciObject->GetSessionData('searchrule')){
			$this->db->like($searchRule);
		}
		if($sort = $this->ciObject->GetSessionData('sort')){
			$this->db->orderby($sort['c'], $sort['r']);
		}
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $this->ciObject->GetPagingDS($query->result());
		return array();
	}
	
	public function GetEmployeersList(){
		$this->db->select('_employeers.rid, CONCAT(_employeers.f_name,\' \',_employeers.s_name, \' \',_employeers.l_name) as employeer_name');
		$this->db->from('_employeers');
		$this->db->orderby('employeer_name');
		$query = $this->DBGet('_employeers');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetPositionsList(){
		$this->db->select('_positions.rid, _positions.name as position_name');
		$this->db->from('_positions');
		$this->db->orderby('position_name');
		$query = $this->DBGet('_positions');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetEditDS(){
		$this->db->select('_documents.rid, 
							DATE_FORMAT(_emp_to_positions_headers.date_doc, \'%d.%m.%Y\') as date_doc,
							_emp_to_positions_rows._employeers_rid as _employeers_rid,
							_emp_to_positions_rows._positions_rid as _positions_rid,
							DATE_FORMAT(_emp_to_positions_rows.bdate, \'%d.%m.%Y\') as bdate,
							CONCAT(_employeers.f_name,\' \',_employeers.s_name,\' \',_employeers.l_name) as employeer_name,
							_positions.name as position_name,
							DATE_FORMAT(_documents.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_emp_to_positions_headers', '_emp_to_positions_headers._documents_rid = _documents.rid');
		$this->db->join('_emp_to_positions_rows', '_emp_to_positions_rows._emp_to_positions_headers_rid = _emp_to_positions_headers.rid');
		$this->db->join('_employeers', '_emp_to_positions_rows._employeers_rid = _employeers.rid');
		$this->db->join('_positions', '_emp_to_positions_rows._positions_rid = _positions.rid');
		$this->db->where(array('_documents.rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $query->row();
		return False;
	}
	
	public function CreateRecord(){
		$insDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->insert('_documents', $insDocumentArray);
		$docRid = $this->db->insert_id();
		$insHeaderArray = array('date_doc'=>$this->toMySQLDate($_POST['date_doc']),
							'_documents_rid'=>$docRid,
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);

		$this->db->insert('_emp_to_positions_headers', $insHeaderArray);
		$insRowsArray = array('_emp_to_positions_headers_rid'=>$this->db->insert_id(),
							'_employeers_rid'=>$_POST['_employeers_rid'],
							'_positions_rid'=>$_POST['_positions_rid'],
							'bdate'=>$this->toMySQLDate($_POST['bdate']),
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->insert('_emp_to_positions_rows', $insRowsArray);
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $docRid;
		}		
	}
	
	public function UpdateRecord(){
		$updateDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->update('_documents', $updateDocumentArray, array('_documents.rid'=>$this->ciObject->GetSessionData('activerecord')));
				
		$updateHeaderArray = array('archive'=>$_POST['archive'],
								'modifier_users_rid'=>$this->userRid,
								'modifyDT'=>$this->nowTime);

		$this->db->update('_emp_to_positions_headers', $updateHeaderArray, array('_emp_to_positions_headers._documents_rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->db->getwhere('_emp_to_positions_headers', array('_emp_to_positions_headers._documents_rid'=>$this->ciObject->GetSessionData('activerecord')));
		if(!$query->num_rows()) {
			$this->db->trans_rollback();
    		return False;
		}
		$row = $query->row();
		$updateRowsArray = array('_employeers_rid'=>$_POST['_employeers_rid'],
							'_positions_rid'=>$_POST['_positions_rid'],
							'bdate'=>$this->toMySQLDate($_POST['bdate']),
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->update('_emp_to_positions_rows', $updateRowsArray, array('_emp_to_positions_rows._emp_to_positions_headers_rid'=>$row->rid));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $this->ciObject->GetSessionData('activerecord');
		}		
	}
	
	public function DeleteRecord(){
		$this->db->trans_begin();
		$this->db->delete('_documents', array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
}
?>